package com.ebeijia.controller;

import com.ebeijia.dto.BindUserDto;
import com.ebeijia.mybatis.pojo.UserManage;
import com.ebeijia.query.MemberQuery;
import com.ebeijia.query.Paginator;
import com.ebeijia.service.admin.BillPushInfoService;
import com.ebeijia.service.admin.UserInfoService;
import com.ebeijia.web.ResponseMessage;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by YPJ on 2016/5/12.
 */
@Controller
@RequestMapping("/member")
public class MemberController extends BaseController {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private BillPushInfoService billPushInfoService;

    @RequestMapping("/toShow.htm")
    public ModelAndView toShow() {
        ModelAndView modelAndView = new ModelAndView("member/memberList");
        return modelAndView;
    }

    @RequestMapping("/memberList.json")
    @ResponseBody
    public ResponseMessage memberList(MemberQuery memberQuery) {
        Paginator<BindUserDto> paginator = new Paginator<BindUserDto>(memberQuery,
            userInfoService.queryBindUser(memberQuery), userInfoService.queryBindUserCount(memberQuery));
        return ResponseMessage.success(paginator);
    }

    @RequestMapping("/toMemberQueryList.htm")
    public ModelAndView toMemberQueryList() {
        ModelAndView modelAndView = new ModelAndView("member/memberQueryList");
        return modelAndView;
    }

    @RequestMapping("/memberQueryList.json")
    @ResponseBody
    public ResponseMessage memberQueryList(MemberQuery memberQuery) {
        Paginator<BindUserDto> paginator = new Paginator<BindUserDto>(memberQuery,
            userInfoService.selectUserList(memberQuery), userInfoService.selectUserListCount(memberQuery));
        return ResponseMessage.success(paginator);
    }

    @RequestMapping("/pushSend.json")
    @ResponseBody
    public ResponseMessage sendPush(@RequestParam("status") String status,
        @RequestParam(value = "userNos", required = false) List<String> userNos) {
        if (CollectionUtils.isEmpty(userNos)) {
            return ResponseMessage.error("101", "未选择");
        }
        if (billPushInfoService.insertBatch(status, userNos) > 0) {

            return ResponseMessage.success();
        } else {
            return ResponseMessage.error();
        }

    }

    @RequestMapping("/canclePush.json")
    @ResponseBody
    public ResponseMessage canclePush(@RequestParam("status") String status,
        @RequestParam(value = "userNos", required = false) List<String> userNos) {
        if (CollectionUtils.isEmpty(userNos)) {
            return ResponseMessage.error("101", "未选择");
        }
        try {
            billPushInfoService.deleteBatch(userNos);
            return ResponseMessage.success();
        } catch (Exception e) {
            return ResponseMessage.error();
        }

    }

}
